10-Selection without ifs demo of DeMorgan's Laws.py


Sign up Free. Don't forget to check out our challenges, lessons, solve and learn series and more ...


Code Snippet

#  Purpose:    Demo of DeMorgan's Laws:


#  1.  a Not And is equivalent to an Or with two negated inputs
#  2.  a Not Or is equivalent to an And with two negated inputs



#  Test data: 0 0, 0 1, 1 0, 1 1
#  For ***any*** value of x and y, (not(x < 15 and y >= 3)) == (x >= 15 or y < 3)
#  Common uses of De Morgan's rules are in digital circuit design
#  where it is used to manipulate the types of logic gates.
#  Also, computer programmers use them to change a complicated statement
#  like IF ... AND (... OR ...) THEN ... into its opposite (and shorter) equivalent.


x = int(input("Enter a value for x: "))
y = int(input("Enter a value for y: "))
print ((not(x < 15 and y >= 3)))
print ((x >= 15 or y < 3))

#example from: http://www.annedawson.net/Python3Programs.txt
                    

Try it yourself